package ii.oo.liu;

import org.junit.Test;

import java.io.*;

/**
 * Created by Admin on 1/3 0003.
 */
public class IOTest {
    @Test
    public void test() throws Exception {
        //读取:字节
        File file = new File("a.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
//        int buf;
        //一次读取一个;
//        while ((buf=fileInputStream.read())!=-1){
//            System.out.println(buf);
//        }

        //一次读取一组;
        byte[] b = new byte[1024];
        int len;
        while ((len = fileInputStream.read(b)) != -1) {
            String str = new String(b, 0, len);
            System.out.println(str);
        }
        fileInputStream.close();
    }

    @Test
    public void test2() throws IOException {
        //输出:字节
        //输出文件可以不存在;若存在,就是覆盖;
        File file = new File("a.txt");
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(new String("Love").getBytes());
        fos.close();
    }

    @Test
    public void test3() throws Exception {
        //从一个文件读取内容,然后写入到另一个文件中去;(相当于文件复制): 字节
        File file = new File("a.txt");
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream("b.txt");
        byte[] buf = new byte[1024];
        int len;
        while ((len = fis.read(buf)) != -1) {
            fos.write(buf, 0, len);
            fos.flush();
        }
        fos.close();
        fis.close();
    }

    @Test
    public void test4() throws IOException {
        //字符: 文件的复制
        File file = new File("a.txt");
        FileReader fr = new FileReader(file);
        char[] c = new char[1024];
        int len;
        while ((len = fr.read(c)) != -1) {
            String str = new String(c, 0, len);
            System.out.println(str);
        }
        fr.close();
    }

    @Test
    public void test5() throws IOException {
        File file = new File("a.txt");
        File file1 = new File("c.txt");
        FileReader fr = new FileReader(file);
        FileWriter fw = new FileWriter(file1);
        char[] c = new char[1024];
        int len;
        while ((len = fr.read(c)) != -1) {
            fw.write(c, 0, len);
        }
        fw.close();
        fr.close();
    }

    @Test
    public void test6() throws IOException {
        //缓冲流: 字节 文件复制
        File file = new File("1.jpg");
        File file1 = new File("2.jpg");
        BufferedInputStream bis =
                new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream bos =
                new BufferedOutputStream(new FileOutputStream(file1));
        byte[] buf=new byte[1024];
        int len;
        while ((len=bis.read())!=-1){
            bos.write(buf,0,len);
            bos.flush();
        }
        bos.close();
        bis.close();

    }

    @Test
    public void test7(){
        //转化流:
        OutputStreamWriter ow=null;
        InputStreamReader is=null;
        BufferedWriter bw= null;
        BufferedReader br= null;
        try {
            ow=new OutputStreamWriter(new FileOutputStream(new File("d.txt")));
            is=new InputStreamReader(new FileInputStream(new File("a.txt")));
            br = new BufferedReader(is);
            bw = new BufferedWriter(ow);
            String str;
            while ((str=br.readLine())!=null){
                bw.write(str);
                bw.newLine();
                bw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
